import sha256 from '../../node_modules/_crypto-js@3.1.9-1@crypto-js/sha256';
export default {
    data () {
        return {
          
            modal:false,
            filter: {
                list:[],
                total:0,
                page:1,
                limit:10,
                name:''
            },
            ids:[],//所有选中的id组成的数组
            
          
        }
    },
    methods: {
        show (row) {
            this.modal = true;
            Object.assign(this.$data.formValidate,row)

        },
        remove (id) {//删除数据
            //确认删除弹窗
            this.$Modal.confirm({
                title: '确认对话框标题',
                content: '<p>确认删除该记录吗？</p>',
                onOk: () => {//点击确认------就删除该数据
                    this.$http.delete(`http://localhost:3000/${this.module}/data/${id}`)//注意此处用的是反引号
                    .then(res=>{
                        this.$Message.info('删除数据成功');
                        this.getData();
                    })
                },
                onCancel: () => {//点击取消------不执行任何操作
                    
                }
            });

            
        },
        getData(){//分离出来专门用于获取数据的
                this.$http.post(`http://localhost:3000/${this.module}/list`,this.filter)
            .then(res=>{
            
                // console.log(res.data)
                this.filter.list = res.data.rows;
                this.filter.total = res.data.total;
                this.filter.page = res.data.page;
            })
        },
        onchange(page){//每页显示的内容
            // console.log(page)
            this.filter.page = page;
            this.getData();
        },
        onPageSizeChange(pagesize){//改变每页显示多少条
            this.filter.limit = pagesize;
            this.getData();
        },
        onSearch(){//查询
            this.getData();
        },
        onSelectionChange(selection){//把选中的id取出放在一个数组中
            if(selection && selection.length>0){
                var ids = [];
                let selectionlen = selection.length;
                for(let i=0; i<selectionlen;i++){
                    ids.push(selection[i]._id)
                }
                this.ids = ids;
            }
        },
        onDeletes(){//删除多条选中的
                //确认删除弹窗
            this.$Modal.confirm({
                title: '确认对话框标题',
                content: '<p>确认删除该记录吗？</p>',
                onOk: () => {//点击确认------就删除该数据
                    this.$http.post(`http://localhost:3000/${this.module}/deletes`,{ids:this.ids.toString()})
                    .then(res=>{
                        this.$Message.info('删除数据成功');
                        this.getData();
                    })
                },
                onCancel: () => {//点击取消------不执行任何操作
                    
                }
            });
            
        },
        onAdd(){//添加
            this.modal = true;
            Object.assign(this.$data.formValidate,this.$options.data().formValidate)//对象合并并且初始表单数据
        },

        //添加的弹窗里的表单提交和重置方法
        handleSubmit (name) {//提交
            this.$refs[name].validate((valid) => {
                if (valid) {//表单验证成功
                    this.formValidate.password = sha256(this.formValidate.password).toString();
                    if(this.formValidate._id && this.formValidate._id.length>0){//如果已经有id就修改
                        this.$http.put(`http://localhost:3000/${this.module}/data/${this.formValidate._id}`,this.formValidate)
                        .then(res=>{
                            this.$Message.success('提交成功!');
                            this.modal = false;
                            this.getData();
                        })
                    }else{//否则就添加
                        this.$http.post(`http://localhost:3000/${this.module}/data`,this.formValidate)
                        .then(res=>{
                        this.$Message.success('提交成功!');
                        this.modal = false;
                        this.getData();
                    })
                    }
                    
                    
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        handleReset (name) {//重置
            this.$refs[name].resetFields();
        }
    },
    created(){
        this.getData();
    }
}

